package com.jbh.zxinglibrary;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import java.util.Hashtable;


/**
 * @author yxj
 *
 * 创建 二维码
 *
 */

public class CreateQRImage {



    /**
     * @param QR_WIDTH    二维码的宽
     * @param QR_HEIGHT   二维码的高
     * @param url         生成二维码的地址
     */
    public static Bitmap createQRImage(int QR_WIDTH, int QR_HEIGHT, String url) {
        int w = QR_WIDTH ;
        int h = QR_HEIGHT;
        try {
            //判断URL合法性
            if (url == null || "".equals(url) || url.length() < 1) {
                return null;
            }
            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            //图像数据转换，使用了矩阵转换
            BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);

            /**处理生成图片白边的问题*/
            bitMatrix = deleteWhite(bitMatrix);//删除白边
            QR_WIDTH = bitMatrix.getWidth();
            QR_HEIGHT = bitMatrix.getHeight();

            /***/
            int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
            //下面这里按照二维码的算法，逐个生成二维码的图片，
            //两个for循环是图片横列扫描的结果
            for (int y = 0; y < QR_HEIGHT; y++) {
                for (int x = 0; x < QR_WIDTH; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * QR_WIDTH + x] = 0xff000000;
                    } else {
                        pixels[y * QR_WIDTH + x] = 0xffffffff;
                    }
                }
            }

            //生成二维码图片的格式，使用ARGB_8888
            Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
            return zoomImg(bitmap ,w,h);
        }
        catch (WriterException e) {
            e.printStackTrace();
        }
        return null ;
    }



    /**
     * @param QR_WIDTH    二维码的宽
     * @param QR_HEIGHT   二维码的高
     * @param url         生成二维码的地址
     */
    public static Bitmap createQRImage(int QR_WIDTH, int QR_HEIGHT, String url, Bitmap insertLogo) {
        int w = QR_WIDTH ;
        int h = QR_HEIGHT;
        try {
            //判断URL合法性
            if (url == null || "".equals(url) || url.length() < 1) {
                return null;
            }
            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            //图像数据转换，使用了矩阵转换
            BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);

            /**处理生成图片白边的问题*/
            bitMatrix = deleteWhite(bitMatrix);//删除白边
            QR_WIDTH = bitMatrix.getWidth();
            QR_HEIGHT = bitMatrix.getHeight();

            /***/
            int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
            //下面这里按照二维码的算法，逐个生成二维码的图片，
            //两个for循环是图片横列扫描的结果
            for (int y = 0; y < QR_HEIGHT; y++) {
                for (int x = 0; x < QR_WIDTH; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * QR_WIDTH + x] = 0xff000000;
                    } else {
                        pixels[y * QR_WIDTH + x] = 0xffffffff;
                    }
                }
            }

            //生成二维码图片的格式，使用ARGB_8888
            Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
            if (null!=insertLogo){
                return   AddLogo(zoomImg(bitmap, w, h),insertLogo);
            }else {
                return zoomImg(bitmap ,w,h);
            }
        }
        catch (WriterException e) {
            e.printStackTrace();
        }
        return null ;
    }


    /**
     * 添加logo
     *
     * @param zoomImg
     * @param insertLogo
     * @return
     */
    private static Bitmap AddLogo(Bitmap zoomImg, Bitmap insertLogo) {
        //二维码的宽
        int w = zoomImg.getWidth() ;
        //二维码的高度
        int h = zoomImg.getHeight() ;
        //创建空bitmap
        Bitmap newB = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        //设置到绘制板上
        Canvas canvas=new Canvas(newB);

        Matrix matrix=new Matrix();
        float scale = (float) (30 * 2.0 / insertLogo.getWidth());
        matrix.postScale(scale,scale);
        matrix.postTranslate((float) ((w*1.0/2) -30), (float) ((h*1.0/2) - 30));

        canvas.save();
        canvas.drawBitmap(zoomImg,0,0,null);
        canvas.drawBitmap(insertLogo,matrix,null);
        canvas.restore();
        return newB ;
    }


    private static Bitmap createCenterBitmap2(BitMatrix bitMatrix, Bitmap bitmap) {
        try {
            int[] rec = bitMatrix.getEnclosingRectangle();
            int left = rec[0];
            int top =  rec[1];
            int width = rec[2];
            int height = rec[3];
            Matrix matrix = new Matrix();
            float scaleValue = 1.0f * bitmap.getWidth() / width ;
            matrix.postScale(scaleValue, scaleValue);// 使用后乘
            return Bitmap.createBitmap(bitmap, left, top, width, height, matrix, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }




    /**
     * 删除二维码的白边
     *
     * @param matrix
     * @return
     */
    private static BitMatrix deleteWhite(BitMatrix matrix) {
        int[] rec = matrix.getEnclosingRectangle();
        int resWidth = rec[2] + 1;
        int resHeight = rec[3] + 1;

        BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
        resMatrix.clear();
        for (int i = 0; i < resWidth; i++) {
            for (int j = 0; j < resHeight; j++) {
                if (matrix.get(i + rec[0], j + rec[1]))
                    resMatrix.set(i, j);
            }
        }
        return resMatrix;
    }



    // 等比缩放图片
    private static Bitmap zoomImg(Bitmap bm, int newWidth , int newHeight){
        // 获得图片的宽高
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }
}
